﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace Xceed.Silverlight.Data
{
  [DebuggerDisplay( "Find = {DebuggerDisplayFind}" )]
  public sealed class FindRequest
  {
    #region Constructor

    internal FindRequest( IEnumerable<FindValue> values, FindPosition position )
    {
      if( values == null )
        throw new ArgumentNullException( "values" );

      if( !values.Any() )
        throw new ArgumentException( "At least one value must be specified.", "values" );

      if( !Enum.IsDefined( typeof( FindPosition ), position ) )
        throw new ArgumentException( "The specified position is invalid.", "position" );

      m_values.AddRange( values );
      m_readOnlyValues = new ReadOnlyCollection<FindValue>( m_values );

      this.Position = position;
    }

    #endregion

    #region Values Property

    public IList<FindValue> Values
    {
      get
      {
        return m_readOnlyValues;
      }
    }

    private ReadOnlyCollection<FindValue> m_readOnlyValues;
    private List<FindValue> m_values = new List<FindValue>();

    #endregion

    #region Position Property

    public FindPosition Position
    {
      get;
      private set;
    }

    #endregion

    #region DebuggerDisplayFind Private Property

    // This property only purpose is to generate a string for the DebuggerDisplay attribute.
    private string DebuggerDisplayFind
    {
      get
      {
        StringBuilder sb = new StringBuilder();
        sb.AppendFormat( "{{{0}}}", this.Position );

        bool isFirstValue = true;
        foreach( FindValue findValue in m_values )
        {
          if( !isFirstValue )
          {
            sb.Append( "," );
          }
          else
          {
            isFirstValue = false;
          }

          object value = findValue.Value;

          sb.Append( " {" );
          if( value == null )
          {
            sb.Append( "#null#" );
          }
          else
          {
            sb.Append( value.ToString() );
          }
          sb.Append( "}" );
        }

        return sb.ToString();
      }
    }

    #endregion
  }
}
